package com.graduate.work.graduatework.service;

import com.graduate.work.graduatework.dao.AirTicketDetailMapper;
import com.graduate.work.graduatework.dao.CommentMapper;
import com.graduate.work.graduatework.dao.UserMapper;
import com.graduate.work.graduatework.entity.AirTicketDetail;
import com.graduate.work.graduatework.entity.Comment;
import com.graduate.work.graduatework.entity.User;
import com.graduate.work.graduatework.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

/**
 * @author: cuibingqi.
 * @date: 17/5/3 下午4:43.
 * @descripe:
 */
@Service
public class DataAcessService {

    public static SqlSessionFactory sqlSessionFactory;
    static {
        sqlSessionFactory = MybatisUtils.getSqlSessionFactory();
    }

    /**
     * 用户注册，将注册数据放入数据库
     * @param user
     * @return
     */
    public boolean userRegister(User user){
        SqlSession sqlSession = sqlSessionFactory.openSession();
        try{
            UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
            userMapper.insertUser(user);
            sqlSession.commit();
        }catch (Exception e){
            e.getMessage();
            return false;
        }finally {
            sqlSession.close();
        }
        return true;
    }

    /**
     * 用户登录，返回登录后的用户信息
     * @param userId
     * @param password
     * @return
     */
    public User userLogin(String userId,String password){
        SqlSession sqlSession = sqlSessionFactory.openSession();
        Optional<User> retUser;
        try{
            UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
            retUser = Optional.ofNullable(userMapper.getUser(userId,password));
            sqlSession.commit();
            if(!retUser.isPresent()){
                return null;
            }
        }catch (Exception e){
            e.getMessage();
            return null;
        }finally {
            sqlSession.close();
        }
        return retUser.get();
    }

    /**
     * 用户收藏航线，将航线信息放入数据库
     * @param airTiketDetailEntity
     * @return
     */
    public boolean collectionOWAirline(AirTicketDetail airTiketDetailEntity){
        SqlSession sqlSession = sqlSessionFactory.openSession();
        try{
            AirTicketDetailMapper airTicketDetailMapper = sqlSession.getMapper(AirTicketDetailMapper.class);
            airTicketDetailMapper.collect(airTiketDetailEntity);
            sqlSession.commit();
        }catch (Exception e){
            e.getMessage();
            return false;
        }finally {
            sqlSession.close();
        }
        return true;
    }

    /**
     * 用户删除收藏的航线
     * @param airTiketDetailEntity
     * @return
     */
    public boolean deleteOWAirline(AirTicketDetail airTiketDetailEntity){
        SqlSession sqlSession = sqlSessionFactory.openSession();
        try{
            AirTicketDetailMapper airTicketDetailMapper = sqlSession.getMapper(AirTicketDetailMapper.class);
            airTicketDetailMapper.deleteCollet(airTiketDetailEntity);
            sqlSession.commit();
        }catch (Exception e){
            e.getMessage();
            return false;
        }finally {
            sqlSession.close();
        }
        return true;
    }

    /**
     * 查询所有收藏的航线
     * @param userId
     * @return
     */
    public List<AirTicketDetail> selectAllOWAirline(String userId){
        SqlSession sqlSession = sqlSessionFactory.openSession();
        List<AirTicketDetail> list = null;
        try{
            AirTicketDetailMapper airTicketDetailMapper = sqlSession.getMapper(AirTicketDetailMapper.class);
            list = airTicketDetailMapper.selectCollect(userId);
            sqlSession.commit();
        }catch (Exception e){
            e.getMessage();
        }finally {
            sqlSession.close();
        }
        return list;
    }

    /**
     * 添加用户评论
     * @param comment
     * @return
     */
    public boolean addComment(Comment comment){
        SqlSession sqlSession = sqlSessionFactory.openSession();
        try{
            CommentMapper commentMapper = sqlSession.getMapper(CommentMapper.class);
            commentMapper.addComment(comment);
            sqlSession.commit();
        }catch (Exception e){
            e.getMessage();
            return false;
        }finally {
            sqlSession.close();
        }
        return true;
    }

    /**
     * 删除用户评论
     * @param id
     * @return
     */
    public boolean deleteComment(int id){
        SqlSession sqlSession = sqlSessionFactory.openSession();
        try{
            CommentMapper commentMapper = sqlSession.getMapper(CommentMapper.class);
            commentMapper.deleteComment(id);
            sqlSession.commit();
        }catch (Exception e){
            e.getMessage();
            return false;
        }finally {
            sqlSession.close();
        }
        return true;
    }
    public List<Comment> selectComment(String flightNumber){
        SqlSession sqlSession = sqlSessionFactory.openSession();
        List<Comment> list = null;
        try{
            CommentMapper commentMapper = sqlSession.getMapper(CommentMapper.class);
            list = commentMapper.getComment(flightNumber);
            sqlSession.commit();
        }catch (Exception e){
            e.getMessage();
        }finally {
            sqlSession.close();
        }
        return list;
    }

}
